package com.duck.duckchatbackend.test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
/**
 * SocketServer类用于创建一个简单的Socket服务端
 * 它监听指定端口，并在接收到客户端连接时打印客户端信息及接收的数据
 */
public class SocketServer {
    /**
     * 程序的入口点
     * 启动Socket服务端，监听客户端连接，并处理接收到的数据
     *
     * @param args 命令行参数，本程序不使用此参数
     */
    public static void main(String[] args) {
        ServerSocket server = null;
        Map<String, Socket> CLIEANT_MAP = new HashMap<>();
        try {
            // 创建ServerSocket实例，监听2077端口
            server = new ServerSocket(2077);
            System.out.println("服务端已启动，等待客户端链接");
            while (true) {
                // 等待并接受客户端连接
                Socket socket = server.accept();
                // 获取并打印客户端的IP地址和端口号
                String ip = socket.getInetAddress().getHostAddress();
                System.out.println("有客户端链接ip："+ ip + ",端口为:" + socket.getPort());
                String clientKey = ip + socket.getPort();
                CLIEANT_MAP.put(clientKey, socket);
                // 启动一个新线程处理客户端数据接收
                new Thread(() -> {
                    while (true) {
                        try {
                            // 从socket中获取输入流，并包装为BufferedReader以便读取
                            InputStream inputStream = socket.getInputStream();
                            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf8");
                            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                            // 读取并打印客户端发送的数据
                            String readData = bufferedReader.readLine();
                            System.out.println("收到客户端消息->" + readData);
                            // 服务端给每个客户端发送数据
                                CLIEANT_MAP.forEach((k, v)->{
                                   try{
                                       // System.out.println(k);
                                       OutputStream outputStream = v.getOutputStream();
                                       PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream,"utf8"));
                                       printWriter.println(socket.getPort() + ":" + readData);
                                       printWriter.flush();
                                   }catch (Exception e){
                                       e.printStackTrace();
                                   }
                                });
//                            // 服务端发送数据
//                            OutputStream outputStream = socket.getOutputStream();
//                            PrintWriter printWriter = new PrintWriter(outputStream);
//                            printWriter.println("我是服务端，我已经收到了你发送的消息：  " + readData);
//                            printWriter.flush();
                        } catch (Exception e) {
                            e.printStackTrace();
                            break;
                        }
                    }
                }).start();
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
}

